/*
 * Sammy.js Plugin for JS-Arch-Core
 * Depends on Sammy.js and TraceKit
 */
Core.createPlugin({
    apps : [],
    init : function (options, modules) {
        var hash = options.hash | '#/',
            i;
        for (i = 0; i < apps.length; i++) {
            apps[i].sammy.run(hash);
        }
    },
    newModule : function (selector, module) {
        if (module.routes) {
            apps[apps.length] = $.sammy(selector, function () {
                var i, type, route, callback;
                // Link Sammy.js Routes
                // Go through every route type ('get', 'post', etc)
                for (i = 0; i < routeTypes.length; i += 1) {
                    type = routeTypes[i];
                    // Check if the module has routes for this type
                    if (module.routes[type]) {
                        // Go through each route for this type
                        for (route in module.routes[type]) {
                            if (module.routes[type].hasOwnProperty(route)) {
                                // Create a proxy route function to call the
                                // handler in the module.
                                this.route(type, route, (function () {
                                    // Get some immediate values from when this
                                    // function is created
                                    var t = type.valueOf(),
                                        r = route.valueOf();
                                    return function (context) {
                                        module.routes[t][r].call(module, context);
                                    };
                                }()));
                            }
                        }
                    }
                }
            });
        }
    }
});